package dao;

import beans.DepartmentsBeanRemote;
import beans.DepartmentsBeanRemoteHome;
import javax.naming.NamingException;
import wrappers.*;
import exceptions.DatabaseException;
import java.rmi.RemoteException;
import java.util.List;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import views.LinkElement;
import views.TextElement;

/**
 *
 * @author KAV
 */
public class DepartmentDAO implements BaseDAO {

    DepartmentsBeanRemoteHome home;

    public DepartmentDAO() {
        try {
            InitialContext ic = new InitialContext();
            Object objBean = ic.lookup("ejb/beanDepartments");
            home = (DepartmentsBeanRemoteHome) PortableRemoteObject.narrow(objBean, DepartmentsBeanRemoteHome.class);
        } catch (NamingException ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public void insert(Object obj) throws DatabaseException {
        try {
            DepartmentWrap e = (DepartmentWrap) obj;
            home.create(e.getTitle(), e.getCountry());
        } catch (CreateException ex) {
            throw new DatabaseException(ex);
        } catch (RemoteException ex) {
            throw new DatabaseException(ex);
        }
    }

    @Override
    public void deleteById(int id) throws DatabaseException {
        try {
            Object o = home.findByPrimaryKey(id);
            DepartmentsBeanRemote cbr = (DepartmentsBeanRemote) o;
            cbr.remove();
        } catch (RemoveException ex) {
            throw new DatabaseException(ex);
        } catch (RemoteException ex) {
            throw new DatabaseException(ex);
        } catch (FinderException ex) {
            throw new DatabaseException(ex);
        }catch (EJBException ex) {
            throw new DatabaseException(ex);
        }
    }

    @Override
    public void updateById(Object obj) throws DatabaseException {
        try {
            DepartmentWrap e = (DepartmentWrap) obj;
            DepartmentsBeanRemote dbr = (DepartmentsBeanRemote) home.findByPrimaryKey(e.getId());
            dbr.setTitle(e.getTitle());
            dbr.setCountry(e.getCountry());
        } catch (FinderException ex) {
            throw new DatabaseException(ex);
        } catch (RemoteException ex) {
            throw new DatabaseException(ex);
        }
    }

    @Override
    public List<Object> findAll() throws DatabaseException {
        List<Object> list = new java.util.LinkedList<Object>();
        try {
            List<Object> container = (List<Object>) home.findAll();
            for (Object o : container) {
                DepartmentsBeanRemote cbr = (DepartmentsBeanRemote) o;
                DepartmentWrap dw = new DepartmentWrap((Integer) cbr.getPrimaryKey(), cbr.getTitle(), cbr.getCountry());
                CountryDAO dao = new CountryDAO();
                CountryWrap cw = (CountryWrap) dao.findById(cbr.getCountry());
                dw.setViewCountry(new LinkElement("country", cw.getTitle(), cw.getCells(), CountryWrap.IDR));
                list.add((Object) dw);
            }
        } catch (RemoteException ex) {
            throw new DatabaseException(ex);
        } catch (FinderException ex) {
            throw new DatabaseException(ex);
        }
        return list;
    }

    @Override
    public Object findById(int id) throws DatabaseException {
        Object obj = new Object();
        try {
            Object o = home.findByPrimaryKey(id);
            DepartmentsBeanRemote cbr = (DepartmentsBeanRemote) o;
            DepartmentWrap dw = new DepartmentWrap((Integer) cbr.getPrimaryKey(), cbr.getTitle(), cbr.getCountry());
            CountryDAO dao = new CountryDAO();
            CountryWrap cw = (CountryWrap) dao.findById(cbr.getCountry());
            dw.setViewCountry(new LinkElement("country", cw.getTitle(), cw.getCells(), CountryWrap.IDR));
            obj = (Object) dw;
        } catch (RemoteException ex) {
            throw new DatabaseException(ex);
        } catch (FinderException ex) {
            throw new DatabaseException(ex);
        }
        return obj;
    }

    @Override
    public List<Object> findByNameOrSurname(String query) throws DatabaseException {
        throw new DatabaseException(new UnsupportedOperationException("Not supported yet."));
    }
}
